import java.util.*;

class a1{
    public static volatile DiskMemory dm=new DiskMemory();
    public static void main(String[] args) {
        List<Thread> tlist=new ArrayList<Thread>(); 
       
        for(int i=0;i<4;i++){
            int ii=i;
            tlist.add(new Thread(new Runnable(){
                @Override
                public void run(){
                   
                    int s=dm.getSize();
                    System.out.println("thread "+ ii + " size: "+s);
                    dm.setSize(s);

                }
            }));
            tlist.get(i).start();
        }
        for(Thread t:tlist){
            try {
                t.join();
            } catch (Exception e) {
                //TODO: handle exception
            }
        }
        System.out.println("thread "+ 5 + " size: "+dm.getTotalSize());
                   

    }



    static class DiskMemory {
        private int totalSize ;
        public int getSize(){
            return (new Random().nextInt(3)+1)*100;//加一是为了防止获取磁盘大小为0，不符合常理
        }
    
        public synchronized void setSize(int size){
            totalSize += size;
        }
    
        public int getTotalSize(){
            return totalSize;
        }
    }
}


